function beforeClickAdminTree(treeId, treeNode) {
		var zTree = $.fn.zTree.getZTreeObj(treeId);
		
		
		//根据部门Id查询管理员
		AdminTree.loadAdminByDeptId(zTree,treeNode,false,null);
		/*if(treeNode.children ==null || treeNode.length == 0){
			
		}else{
			zTree.checkNode(treeNode, !treeNode.checked, null, true);
			return false;
		}*/
	}
		
function onCheckAdminTree(e, treeId, treeNode){
	AdminTree.viewCallBack();
}

function expandAdminTree(treeId, treeNode) {
	 beforeClickAdminTree(treeId, treeNode);
}
		
/**
* 加载部门和用户的ztree
*/
var AdminTree = {
		_tree_id : null,
		_detp_url : null,
		_admin_url : null,
		_admin_query_url : null,
		_initData : null,//待初始化的数据：[{deptId:1,userId:1},{deptId:2,userId:1}]
		_callback :null,
		_cach_deptId : {},
		//全局设置
		_setting : {
				check: {
					enable: true,
				},
				view: {
					dblClickExpand: true
				},
				data: {
					simpleData: {
						enable: true
					}
				},
				callback: {
					beforeClick: beforeClickAdminTree,
					onCheck: onCheckAdminTree,
					beforeExpand: expandAdminTree
				}
			},
		
		init:function(treeId,deptUrl,adminUrl,adminQueryUrl,chkStyle,initData,callback){
			this._tree_id = treeId;
			this._detp_url = deptUrl;
			this._admin_url = adminUrl;
			this._admin_query_url = adminQueryUrl;
			this._initData = initData;
			this._callback = callback;
			if (chkStyle == "radio") {
				this._setting.check.chkStyle = chkStyle;
				this._setting.check.radioType = "all";
			} else {
				this._setting.check.chkStyle = "checkbox";
				this._setting.check.chkboxType = { "Y" : "ps", "N" : "ps" };
			}
			
			this.loadDeptTree();
			
			this.initSearcheByAdminName();
		},
		
		//加载部门节点
		loadDeptTree : function(){
			var nodeData = null;
			jQuery.ajax({
				url: AdminTree._detp_url,
				type:"post",
				/*async: false,*/
				dataType:"json",
				success: function(data){
					if(data && data.length > 0){
						if (AdminTree._setting.check.chkStyle == "radio") {
							$.each(data,function(i,n){
								data[i].nocheck = true;
							});
						}
						
						$.each(data,function(i,n){
							if(!AdminTree._admin_url){
								data[i].isParent = false;
							}else{
								data[i].isParent = true;
							}
						});
						
						$.fn.zTree.init($("#" + AdminTree._tree_id), AdminTree._setting, data);
						
						AdminTree.loadInitData();
					}else{
						alert("加载管理员失败");
					}
				},
				error:function(){
					alert("加载管理员失败");
				}
			});
		},
		
		//加载初始化数据
		loadInitData : function(){
			var initData = this._initData;
			if(initData == null || initData.length  == 0){
				return ;
			}
			//将所有的初始化数据按照部门id分组
			var initDataArray = {};
			for(var i=0;i<initData.length;i++){
				var aData = initData[i];
				var deptId = aData["deptId"];
				var userId = aData["userId"];
				if(!deptId || !userId){
					continue;
				}
				deptId = "d" + deptId;
				userId = "a" + userId;
				if(initDataArray[deptId]){
					initDataArray[deptId].push(userId);
				}else{
					var userIdArray=new Array()
					userIdArray[0]=userId;
					initDataArray[deptId] = userIdArray;
				}
			}
			var zTree = this.getZtreeObject();
			
			for(var deptId in initDataArray){
				
				var deptNode = zTree.getNodeByParam("id",deptId,null);
				if(deptNode == null){
					continue;
				}
				
				//获取父节点
				var parentNode = deptNode.getParentNode();
				while(!!parentNode){
					//加载管理员
					this.loadAdminByDeptId(zTree,parentNode,true,null);
					
					parentNode = parentNode.getParentNode();
					if(!parentNode){
						break;
					}
				}
				
				var userIdArray = initDataArray[deptId];
				
				//加载管理员
				this.loadAdminByDeptId(zTree,deptNode,true,userIdArray);
				//展开部门的节点
				deptNode.isParent = true;
				
				zTree.checkNode(deptNode, true, null, true);
				if(!deptNode.open){
					zTree.expandNode(deptNode);
				}
			}
		},
		selectInitNodeId : function(zTree,initNodeId){
			if(initNodeId != undefined && initNodeId != null && initNodeId.length > 0){
				for(var i=0;i<initNodeId.length;i++){
					var nodeId = initNodeId[i];
					var userNode = zTree.getNodeByParam("id",nodeId,null);
					if(userNode == null || userNode.checked){
						continue;
					}
					//设置选中复选框
					zTree.checkNode(userNode);
				}
				AdminTree.viewCallBack();
			}
			
			
		},
		//加载管理员信息(isExpand:加载完成之后是否展开节点,初始化需要选中的节点)
		loadAdminByDeptId : function (zTree,treeNode,isExpand,initNodeId){
			if(!AdminTree._admin_url){
				return ;
			}
			var deptId = treeNode["id"];
			
			//判断当前部门节点是否已经加载或管理员节点
			if(this._cach_deptId[deptId] == true){
				AdminTree.selectInitNodeId(zTree,initNodeId);
				return;
			}else{
				this._cach_deptId[deptId] = true;
			}
			
			if(deptId.indexOf("d") == 0){
				deptId = deptId.substring(1);
			}else if(deptId.indexOf("a") == 0){
				return;
			}
			jQuery.ajax({
				url: AdminTree._admin_url,
				type:"post",
				data : {"deptId":deptId},
				dataType:"json",
				async:false,
				success: function(data){
					//增加用户的节点
					if(data && data.length > 0){
						for(var i = 0;i<data.length ;i++){
							var treeData = data[i];
							treeData["checked"] = treeNode.checked ;
								
							//追加子节点
							zTree.addNodes(treeNode,treeData,true);
						}
						if(isExpand == true && !treeNode.open){
							zTree.expandNode(treeNode);
						}
						
						AdminTree.selectInitNodeId(zTree,initNodeId);
						
						AdminTree.viewCallBack();
						
						zTree.reAsyncChildNodes(treeNode, "refresh");
					}
				},
				error:function(){
					msg = "加载管理员异常";
				}
			});
		},
		
		//获取选中的管理员
		getCheckAdmin : function(){
			var treeObj = this.getZtreeObject();
			var checkedNode = treeObj.getCheckedNodes(true);
			
			var admins = [];
			
			if(checkedNode.length > 0){
				for(var i=0;i<checkedNode.length;i++){
					var parentNode = checkedNode[i].getParentNode();
					if(parentNode == null){
						continue;
					}
					var id = checkedNode[i]["id"];
					//判断是否是管理员的节点
					if(id.indexOf("a") == -1){
						continue;
					}else{
						id = this.doSubstringId(id);//id.substring(1);
					}
					var deptId =  this.doSubstringId(parentNode["id"]);
					
					var obj = {};
					obj["deptId"] = deptId;
					obj["deptName"] = parentNode["name"];
					obj["userId"] = id;
					obj["userName"] = checkedNode[i]["name"];
					
					admins.push(obj);
		        }
			}
			return admins;
		},
		doSubstringId : function(id){
			if(!id){
				return "";
			}
			if(id.indexOf("a") == 0 || id.indexOf("d") == 0){
				return id.substring(1);
			}else{
				return id;
			}
		},
		getZtreeObject : function(){
			return $.fn.zTree.getZTreeObj(this._tree_id);
		},
		
		viewCallBack : function(){
			var zTree = $.fn.zTree.getZTreeObj(this._tree_id);
			var nodes = zTree.getCheckedNodes(true);
			var v = "";
			var vId = "";
			
			for (var i=0, l=nodes.length; i<l; i++) {
				var id = nodes[i].id;
				
				//判断选择的是部门
				if(AdminTree._admin_url){
					if(id.indexOf("d") != -1){
						continue;
					}
				}
				
				if(v.length > 0){
					v += " ，";
					vId += ",";
				}
				v += nodes[i].name ;
				vId += id.substring(1) ;
			}
			var inputId = this._tree_id + "_input";
			$("#" + inputId).val(v).change();//赋值并触发change事件
			$("#" + inputId + "_id").val(vId);
		},
		
		//加载搜索管理员的功能
		initSearcheByAdminName : function() {
			if (!$("#searche_adminName")[0]) {
				return;
			}
			$("#searche_adminName").val("请输入姓名，按回车键进行搜索…").css({"color":"#aaa"});
			//获取焦点
			$("#searche_adminName").focus(function(){
				var _text = $(this).val();
				if(_text.indexOf("请输入") >= 0){
					$(this).val("").css({"color":"#000"})
				}
			})
			//失去焦点
			$("#searche_adminName").blur(function(){
				var _text = $(this).val();
				if(_text === ""){
					$(this).val("请输入姓名，按回车键进行搜索…").css({"color":"#aaa"});
				}
			})
			
			$("#searche_adminName").keydown(function(event) {
				if (event.keyCode == 13) {
					$("#queryAdminList").empty().hide();
					var searchName = $("#searche_adminName").val();
					if(!searchName){
						return false;
					}
					
					$("#queryAdminList").show().html('<p style="margin:0 0 2px;padding-left:10px;color: red;">正在查询……</p>');
					
					jQuery.ajax({
						url: AdminTree._admin_query_url,
						type:"post",
						data : {"adminName":searchName},
						dataType:"json",
						success: function(data){
							$("#queryAdminList").empty().hide();
							
							var zTree = AdminTree.getZtreeObject();
							var _div = $("#queryAdminList");
							var _html = "";
							//增加用户的节点
							if(data && data.length > 0){
								for(var i=0;i<data.length;i++){
									var deptId = "d"+data[i]["pid"];
									
									var deptNode = zTree.getNodeByParam("id",deptId,null);
									if(deptNode){
										
										_html += '<p class="abled" style="margin:0 0 2px;padding-left:10px;cursor: pointer;" userId="'+data[i]["id"]+'" deptId="'+data[i]["pid"]+'">'+data[i]["name"]+'('+deptNode.name+')</p>';
									}else{
										_html += '<p style="margin:0 0 2px;padding-left:10px;color:#aaa;" userId="'+data[i]["id"]+'" deptId="'+data[i]["pid"]+'">'+data[i]["name"]+'(部门不存在或被禁用)</p>';
									}
								}
								
								_div.html(_html);
								
								_div.children("p.abled").hover(function(){
									$(this).css({"background-color": "#2dc3e8"});
								},function(){
									$(this).css({"background-color": "#fff"});
								});
								
								_div.children("p.abled").click(function(){
									var deptId = "d"+$(this).attr("deptId");
									var userId = "a"+$(this).attr("userId");
									
									var userIdArray = [userId];
									
									var deptNode = zTree.getNodeByParam("id",deptId,null);
									//展开部门的节点
									deptNode.isParent = false;
									if(!deptNode.open){
										zTree.expandNode(deptNode);
									}
									
									//加载管理员
									AdminTree.loadAdminByDeptId(zTree,deptNode,true,userIdArray);
									zTree.checkNode(deptNode, true, null, true);
									
									$("#queryAdminList").hide();
									
									if(AdminTree._callback != undefined && typeof AdminTree._callback == 'function') {
										AdminTree._callback();
									}
								})
							}else{
								_html += '<p style="margin:0 0 2px;padding-left:10px;">没有查询到管理员</p>';
								_div.html(_html);
							}
							
							_div.slideDown("fast");

						},
						error:function(){
							$("#queryAdminList").html('<p style="margin:0 0 2px;padding-left:10px;color: red;">很抱歉，查询管理员异常！</p>');
						}
					});
				}
			});
	
		}
		
}